import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:talking/tools/config.dart';

class Home extends StatefulWidget {
  @override
  HomePage createState() => HomePage();
}

class HomePage extends State<Home> {
  var _currentIndex = 0;
  var draw = [
    {"title": "编辑资料", "subTitle": "修改头像,昵称"},
    {"title": "占用位置", "subTitle": "占用位置"},
    {"title": "占用位置", "subTitle": "占用位置"},
    {"title": "占用位置", "subTitle": "占用位置"},
    {"title": "占用位置", "subTitle": "占用位置"},
    {"title": "占用位置", "subTitle": "占用位置"},
  ];

  late var email=null;

  late String nickname="";

  @override
  void initState() {
    super.initState();
  refresh();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("消息"),
      ),
      body: Container(),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.message), label: "消息"),
          BottomNavigationBarItem(icon: Icon(Icons.people), label: "好友"),
          BottomNavigationBarItem(
              icon: Icon(Icons.dynamic_form), label: "动态"),
        ],
        currentIndex: _currentIndex,
        selectedItemColor: Colors.blue,
        unselectedItemColor: Colors.grey,
        onTap: (i) {
          setState(() {
            _currentIndex = i;
          });
        },
      ),
      drawer: Drawer(
          child: Column(
            children: [
              SizedBox(
                height: 180,
                width: double.infinity,
                child: Stack(
                  children: [
                    Opacity(
                      opacity: 0.9,
                      child: Container(
                        child: Image.network(
                            "${Constant.BASE_URL}files/image/bg.png"),
                      ),
                    ),
                    Container(
                      height: double.infinity,
                      width: double.infinity,
                      child: Column(
                        children: [
                          CircleAvatar(
                            radius: 30,
                            backgroundImage:circleAvatar(),
                          ),
                          Padding(
                            padding: EdgeInsets.only(bottom: 5),
                            child: Text(
                              nickname,
                              style:
                                  TextStyle(color: Colors.white, fontSize: 20),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(bottom: 20),
                            child: Text(
                              "2506564278@qq.com",
                              style: TextStyle(
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ],
                        mainAxisAlignment: MainAxisAlignment.end,
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: Column(
                  children: [
                    Expanded(
                      child: Container(
                        child: ListView.builder(
                          itemCount: 5,
                          itemBuilder: (c, i) {
                            return ListTile(
                              title: Text(draw[i]["title"].toString()),
                              subtitle: Text(draw[i]["subTitle"].toString()),
                              onTap: () {
                                if (i == 0) {
                                  Navigator.of(context).pushNamed('account').then((value){
                                    imageCache!.clear();
                                    refresh();
                                  });
                                }
                              },
                            );
                          },
                        ),
                      ),
                    ),
                    Container(
                      color: Colors.white,
                      width: double.infinity,
                      child: Padding(
                          padding: EdgeInsets.all(10),
                          child: Row(
                            children: [
                              Icon(Icons.settings),
                              InkWell(
                                child: Icon(Icons.exit_to_app),
                                onTap: () async {
                                  SharedPreferences prefs =
                                      await SharedPreferences.getInstance();
                                  await prefs.setBool('autoLogin', false);
                                  Navigator.of(context)
                                      .pushReplacementNamed('login');
                                },
                              ),
                            ],
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                          )),
                    ),
                  ],
                  mainAxisAlignment: MainAxisAlignment.end,
                ),
              ),
            ],
          )),
    );
  }

  circleAvatar() {
    if(email!=null){
      return NetworkImage("${Constant.BASE_URL}files/icon/${email}.png?t=${Constant.getTimeStamp()}");
    }else{
      return null;
    }
  }

  void refresh() {
    Future.delayed(Duration.zero,()  async {
      SharedPreferences sp =await SharedPreferences.getInstance();
      email=sp.getString("email");
      Dio dio=Dio();
      var res=(await dio.get("${Constant.BASE_URL}user/getNickname/?email=${email}")).data.toString();
      setState(() {
        if(res=="该用户未注册"){
          nickname="";
        }else{
          nickname=res;
        }
      });
    });
  }
}
